/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package net.gis.eemc.dao;

import net.gis.eemc.domain.Faculty;
import net.gis.eemc.domain.HibernateUtil;

import java.util.List;
import org.hibernate.Query;
import org.hibernate.Session;

/**
 *
 * @author Sergey
 */
public class FacultyDAOImpl implements FacultyDAO {

    Session session = null;

    public FacultyDAOImpl() {
        this.session = HibernateUtil.getSessionFactory().getCurrentSession();
    }

    @Override
    public void addFaculty(Faculty fac) {
        org.hibernate.Transaction tx = null;
        this.session = HibernateUtil.getSessionFactory().getCurrentSession();
        try {
            tx = session.beginTransaction();
            session.save(fac);
            tx.commit();
        } catch (Exception e) {
            if (tx != null) {
                tx.rollback();
            }
            e.printStackTrace();
        }
    }

    @Override
    public void updateFaculty(Faculty fac) {
        org.hibernate.Transaction tx = null;
        this.session = HibernateUtil.getSessionFactory().getCurrentSession();
        try {
            tx = session.beginTransaction();
            session.update(fac);
            tx.commit();
        } catch (Exception e) {
            if (tx != null) {
                tx.rollback();
            }
            e.printStackTrace();
        }
    }

    @Override
    public List<Faculty> listFaculty() {
        return getFaculties("from Faculty as faculty");
    }

    @Override
    public List<Faculty> getFacultyWithTitle(String Title) {
        return getFaculties("from Faculty as faculty where faculty.facultyName=\"" + Title + "\"");
    }

    @Override
    public List<Faculty> getFacultyWithShortName(String shortName) {
        return getFaculties("from Faculty as faculty where faculty.shortName=\"" + shortName + "\"");
    }

    @Override
    public void removeFaculty(Integer fac_id) {
        org.hibernate.Transaction tx = null;

        this.session = HibernateUtil.getSessionFactory().getCurrentSession();
        try {
            tx = session.beginTransaction();
            String hqlDelete = "delete Faculty f where f.idfaculty = :id";
            Query q = session.createQuery(hqlDelete);
            q.setString("id", fac_id.toString());
            q.executeUpdate();
            tx.commit();
        } catch (Exception e) {
            if (tx != null) {
                tx.rollback();
            }
            e.printStackTrace();
        }
    }

    private List<Faculty> getFaculties(String query) {
        List<Faculty> facList = null;
        org.hibernate.Transaction tx = null;

        this.session = HibernateUtil.getSessionFactory().getCurrentSession();
        try {
            tx = session.beginTransaction();
            Query q = session.createQuery(query);
            facList = (List<Faculty>) q.list();
            tx.commit();
        } catch (Exception e) {
            if (tx != null) {
                tx.rollback();
            }
            e.printStackTrace();
        }
        return facList;
    }
}
